package org.mashart.pink.m.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDletStateChangeException;

import javax.microedition.lcdui.StringItem;
import org.mashart.pink.m.PinkrMIDlet;
import org.mashart.pink.m.store.Preferences;

/**
 * GUI for configuring the preferences.
 */
public class PreferencesGUI implements CommandListener {
    
    private static final String kUser = "user";
    private static final String kPassword = "password";
    private static final String kDeviceType = "devType";
    private static final String kBtConnectionURL = "btUrl";
    private static final String tBuiltin = "in";
    private static final String tBluetooth = "bt";
    private Form mForm;
    private TextField mUserField, mPasswordField, mDeviceTypeField, mBtConnectionUrlField;


    /**
     * Parent of the Gui: the very MIDlet class instance.
     */
    private PinkrMIDlet midlet;

    /**
     * We keep reference to the mobile device display so not to call
     * <code>Display.getDisplay(midlet)</code> every time we need it.
     *
     * Used to display the current <code>Canvas</code> or <code>Form</code>
     * to the user.
     */
    private Display display;

    /**
     * Preferences persistences storage
     */
    Preferences pref;
    
    private final Command NEXT_CMD = new Command("Next", Command.OK, 1);
    private final Command FINISH_CMD = new Command("OK", Command.OK, 1);

    /**
     * Constructor
     */
    public PreferencesGUI(){
        pref = Preferences.getInstance();
    }

    /**
     * Set the midlet
     */
    public void setMidlet(PinkrMIDlet parent){
        this.midlet = parent;
        this.display = Display.getDisplay(midlet);
    }

    /**
     * Entrance of preferences editor
     */
    public void showPreferencesEditor(boolean forced){
        // if forced to edit the preferences, or the "user" is not set yet
        if( forced || pref.get("user") == null){
            editUser();
        }else{
            String type = pref.get(kDeviceType);
            String url = pref.get(kBtConnectionURL);
            this.midlet.setDevice(type, url);
        }
    }

    /**
     * Edit the user id and password
     */
    public void editUser(){
        mForm = new Form("User");
        mUserField = new TextField("Name", pref.get(kUser), 32, 0);
        mPasswordField = new TextField("Password", pref.get(kPassword), 32, TextField.PASSWORD);
        mForm.append(mUserField);
        mForm.append(mPasswordField);

        mForm.addCommand(FINISH_CMD);
        mForm.setCommandListener(this);
        display.setCurrent(mForm);
    }

    /**
     *
     */
    public void editDevice(){
        mForm = new Form("GPS Device");
        mDeviceTypeField = new TextField("GPS Device", pref.get(kDeviceType), 32, 0);
        mPasswordField = new TextField("Password", pref.get(kBtConnectionURL), 32, 0);
        
        mForm.addCommand(FINISH_CMD);
        mForm.setCommandListener(this);
        display.setCurrent(mForm);
    }

    public void commandAction(Command c, Displayable s) {
        if (c == NEXT_CMD) {
            editDevice();
        }
        else if (c == FINISH_CMD) {
                // Save the user name and password.
            pref.put(kUser, mUserField.getString());
            pref.put(kPassword, mPasswordField.getString());
            pref.save();
            this.midlet.discoverDeviceTypes();
        }
    }

}
